package org.apache.james.mime4j.message;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.field.ContentTypeField;
import org.apache.james.mime4j.field.Field;
import org.apache.james.mime4j.parser.MimeStreamParser;
import org.apache.james.mime4j.util.CharArrayBuffer;
import org.apache.james.mime4j.util.CharsetUtil;
import org.apache.james.mime4j.util.MessageUtils;

public class Header
{
  private Map fieldMap;
  private List fields = new LinkedList();

  public Header()
  {
    this.fieldMap = new HashMap();
  }

  public Header(InputStream paramInputStream)
    throws MimeException, IOException
  {
    this.fieldMap = new HashMap();
    MimeStreamParser localMimeStreamParser = new MimeStreamParser();
    localMimeStreamParser.setContentHandler(new 1(this, localMimeStreamParser));
    localMimeStreamParser.parse(paramInputStream);
  }

  public void addField(Field paramField)
  {
    Object localObject = (List)this.fieldMap.get(paramField.getName().toLowerCase());
    if (localObject == null)
    {
      localObject = new LinkedList();
      this.fieldMap.put(paramField.getName().toLowerCase(), localObject);
    }
    ((List)localObject).add(paramField);
    this.fields.add(paramField);
  }

  public Field getField(String paramString)
  {
    Field localField;
    List localList = (List)this.fieldMap.get(paramString.toLowerCase());
    if ((localList != null) && (!(localList.isEmpty())))
      localField = (Field)localList.get(0);
    while (true)
    {
      return localField;
      localField = null;
    }
  }

  public List getFields()
  {
    return Collections.unmodifiableList(this.fields);
  }

  public List getFields(String paramString)
  {
    List localList2;
    String str = paramString.toLowerCase();
    List localList1 = (List)this.fieldMap.get(str);
    if ((localList1 == null) || (localList1.isEmpty()))
      localList2 = Collections.EMPTY_LIST;
    while (true)
    {
      return localList2;
      localList2 = Collections.unmodifiableList(localList1);
    }
  }

  public int removeFields(String paramString)
  {
    int i;
    String str = paramString.toLowerCase();
    List localList = (List)this.fieldMap.remove(str);
    if ((localList == null) || (localList.isEmpty()))
      i = 0;
    while (true)
    {
      return i;
      Iterator localIterator = this.fields.iterator();
      while (true)
      {
        do
          if (!(localIterator.hasNext()))
            break label89;
        while (!(((Field)localIterator.next()).getName().equalsIgnoreCase(paramString)));
        localIterator.remove();
      }
      label89: i = localList.size();
    }
  }

  public void setField(Field paramField)
  {
    String str = paramField.getName().toLowerCase();
    List localList = (List)this.fieldMap.get(str);
    if ((localList == null) || (localList.isEmpty()))
      addField(paramField);
    while (true)
    {
      return;
      localList.clear();
      localList.add(paramField);
      int i = -1;
      int j = 0;
      Iterator localIterator = this.fields.iterator();
      while (localIterator.hasNext())
      {
        if (((Field)localIterator.next()).getName().equalsIgnoreCase(paramField.getName()))
        {
          localIterator.remove();
          if (i == -1)
            i = j;
        }
        ++j;
      }
      this.fields.add(i, paramField);
    }
  }

  public String toString()
  {
    CharArrayBuffer localCharArrayBuffer = new CharArrayBuffer(128);
    Iterator localIterator = this.fields.iterator();
    while (localIterator.hasNext())
    {
      localCharArrayBuffer.append(localIterator.next().toString());
      localCharArrayBuffer.append("\r\n");
    }
    return localCharArrayBuffer.toString();
  }

  public void writeTo(OutputStream paramOutputStream, int paramInt)
    throws IOException, MimeException
  {
    Charset localCharset;
    BufferedWriter localBufferedWriter;
    Iterator localIterator;
    ContentTypeField localContentTypeField;
    if (paramInt == 3)
    {
      localContentTypeField = (ContentTypeField)getField("Content-Type");
      if (localContentTypeField == null)
      {
        localCharset = MessageUtils.DEFAULT_CHARSET;
        localBufferedWriter = new BufferedWriter(new OutputStreamWriter(paramOutputStream, localCharset), 8192);
        localIterator = this.fields.iterator();
      }
    }
    while (true)
    {
      String str1;
      while (true)
      {
        while (true)
        {
          while (true)
          {
            if (!(localIterator.hasNext()))
              break label185;
            str1 = ((Field)localIterator.next()).toString();
            if ((paramInt != 2) || (MessageUtils.isASCII(str1)))
              break label168;
            throw new MimeException("Header '" + str1 + "' violates RFC 822");
            String str2 = localContentTypeField.getCharset();
            if ((localContentTypeField == null) || (str2 == null))
              break;
            localCharset = CharsetUtil.getCharset(str2);
          }
          localCharset = MessageUtils.ISO_8859_1;
        }
        localCharset = MessageUtils.DEFAULT_CHARSET;
      }
      label168: localBufferedWriter.write(str1);
      localBufferedWriter.write("\r\n");
    }
    label185: localBufferedWriter.write("\r\n");
    localBufferedWriter.flush();
  }
}